package com.example.admin.testdemo.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.admin.testdemo.R;
import com.example.admin.testdemo.application.App;
import com.example.admin.testdemo.constant.Constant;
import com.example.admin.testdemo.util.FileUtil;

import java.io.DataOutputStream;
import java.io.IOException;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private Button btn_serial;
    private Button btn_thj;
    private Button btn_thjold;
    private Button btn_gzjold;
    private Button btn_result;
    private Button btn_zlgy;
    private Button btn_mn;
    private static int reqCode = 1120;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initPermission();
    }

    private void initPermission() {
        String[] permissions = new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this, permissions[0])
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, reqCode);
                Log.e("aaaa","申请权限");
            } else {
                Log.e("aaaa","已经授权");
                initLogConfig();
            }
        } else {
            initLogConfig();
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void initLogConfig() {
        if(!FileUtil.isExists(Constant.LOG_PATH)){
            FileUtil.createFolder(Constant.LOG_PATH);
            Log.e("aaaa","创建成功");
        }else {
            Log.e("aaaa","已经创建");
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        SharedPreferences sp = App.getInstance().getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
        String path = sp.getString("DEVICE", "/dev/ttyS3");
        int baudrate = Integer.decode(sp.getString("BAUDRATE", "9600"));
        btn_serial.setText("串口设置（" + path + " : " + baudrate + ")");
    }

    private void initView() {
        btn_serial = findViewById(R.id.btn_serial);
        btn_serial.setOnClickListener(this);
        btn_zlgy = findViewById(R.id.btn_zlgy);
        btn_zlgy.setOnClickListener(this);
        btn_thj = findViewById(R.id.btn_thj);
        btn_thj.setOnClickListener(this);
        btn_thjold = findViewById(R.id.btn_thjold);
        btn_thjold.setOnClickListener(this);
        btn_gzjold = findViewById(R.id.btn_gzjold);
        btn_gzjold.setOnClickListener(this);
        btn_result = findViewById(R.id.btn_result);
        btn_result.setOnClickListener(this);
        btn_mn = findViewById(R.id.btn_mn);
        btn_mn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_serial:
                nextToActivity(SerialPortPreferences.class);
                break;
            case R.id.btn_thj:
                nextToActivity(ThjTestActivity.class);
                break;
            case R.id.btn_thjold:
                nextToActivity(ThjOldActivity.class);
                break;
            case R.id.btn_gzjold:
                nextToActivity(GzGOldActivity.class);
                break;
            case R.id.btn_result:
                nextToActivity(SerialConnectActivity.class);
//                restartApp();
                break;
            case R.id.btn_zlgy:
                nextToActivity(WeightDeviceActivity.class);
                break;
            case R.id.btn_mn:
                nextToActivity(WeightShopActivity.class);
                break;
            default:
        }
    }

    private void restartApp(){
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream out = new DataOutputStream(
                    process.getOutputStream());
            out.writeBytes("reboot \n");
            out.writeBytes("exit\n");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void nextToActivity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }
}
